package com.yuchen.ishop.web.app;

import com.yuchen.core.MyPager;
import com.yuchen.ishop.entity.GoodsImg;
import com.yuchen.ishop.entity.Seller;
import com.yuchen.ishop.entity.SellerXiaoer;
import com.yuchen.ishop.service.goods.GoodsContentRestClient;
import com.yuchen.ishop.service.goods.GoodsImgRestClient;
import com.yuchen.ishop.service.goods.GoodsRestClient;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.service.seller.ShopRestClient;
import com.yuchen.ishop.service.seller.XiaoerRestClient;
import com.yuchen.ishop.web.BaseAction;
import com.yuchen.utils.fastjson.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品详情
 */
@Controller
public class GoodsDetailAction extends BaseAction {

    @Autowired
    private GoodsRestClient goodsRestClient;

    @Autowired
    private GoodsContentRestClient goodsContentRestClient;

    @Autowired
    private SellerRestClient sellerRestClient;

    @Autowired
    private ShopRestClient shopRestClient;

    @Autowired
    private GoodsImgRestClient goodsImgRestClient;

    @Autowired
    private XiaoerRestClient xiaoerRestClient;

    @RequestMapping(value = {"detail"})
    public String detail(Long id, ModelMap modelMap, HttpServletRequest request) {
        modelMap.addAttribute("myNav","guide");
        Map map = new HashMap(2);
        map.put("id", id);
        map.put("pageSize", 1);
        MyPager myPager = goodsRestClient.findByLimit(map);
        if (myPager != null) {
            List records = myPager.getRecords();
            if (records != null && records.size() > 0){
                Map goods = (Map)records.get(0);
                Long goodsId =Long.valueOf(goods.get("id").toString());
                modelMap.addAttribute("goods", goods);
                modelMap.addAttribute("goodsContent", goodsContentRestClient.get(goodsId));
                String json = goodsImgRestClient.showGoodsImg(goodsId).getJson();
                List<GoodsImg> goodsImgs = JsonUtils.parseArray(json,GoodsImg.class) ;

                //商家 店铺
                if(goods.get("seller_id")!=null){
                    Long seller_id =Long.valueOf(goods.get("seller_id").toString());
                    Seller seller =  sellerRestClient.getById(seller_id);
                    if(seller != null){
                        seller.setPassword(null);
                        seller.setSalt(null);
                        modelMap.addAttribute("seller",  seller);
                        modelMap.addAttribute("shop",  shopRestClient.findShopBySellerId(seller.getId()));
                    }
                }
                //小二
                if(goods.get("xiaoer_id")!=null){
                    Long xiaoer_id =Long.valueOf(goods.get("xiaoer_id").toString());
                    SellerXiaoer xiaoer =  xiaoerRestClient.getXiaoer(xiaoer_id);
                    xiaoer.setPassword(null);
                    xiaoer.setSalt(null);
                    if(xiaoer != null){
                        modelMap.addAttribute("xiaoer",  xiaoer);
                    }
                }
                modelMap.addAttribute("goodsImgs",goodsImgs);
            }

        }
        return "detail";
    }
}
